<template>
	<view class="page">

		<scroll-view scroll-y scroll-with-animation style="height: calc(100vh - 50px);" :show-scrollbar="false">
			<!-- <view class="fixed-title  text-center pt-2 pb-2 font-14 text-white ">我的</view> -->
			<view class="bg-linear-gradient">
				<view class="">
					<free-list-item :border="false" @click="open('tabbar/user/user',ifdUser.id)" title="xgw"
						coverSize="100" showRight showLeftIcon :showRights="false">				
							<u-image  :src="getFullImageUrl(ifdUser.avatar)" border-radius="100rpx"
								style="width: 60px; height: 60px; margin-right: 10px;" @error="handleImageError"></u-image>
						
						<view v-if='userInfo.parentId === null' class="flex flex-column" >
							<view class="text-dark font-16 wrap">{{ifdUser.username}}</view>
							<view class="text-hover-dark font-14 mt-2">手机号：{{ifdUser.phone}}</view>
						</view>
						<view v-else class="flex flex-column">
							<view class="text-dark font-16 wrap">{{userInfo.username}}</view>
							<view class="text-hover-dark font-14 mt-2">手机号：{{userInfo.phone}}</view>
						</view>
					</free-list-item>
				</view>
				<view class="pl-3 pr-3 dfex">
					<view class="dfes font-12 text-FFC251" @click="open('tabbar/user/tag')">
						<view class="dfes" v-for="(item, index) in label.slice(0, 3)" :key="index">
							<view class=" mr-2">{{item}}</view>
						</view>
						<view class="iconfont icon-icon-arrow-right2 font-12"></view>
					</view>
					<view v-if='userInfo.parentId === null'>
						<u-button @click="open('tabbar/user/credit')" size="mini"
							style="background-color: #8ED0F2; color: #fff;">积分任务</u-button>
					</view>
				</view>
				<!-- <view class="pl-3 dfes pb-5" v-else @click="open('login/login')">
					<text class="iconfont icon-wode2 font-48 text-white"></text>
					<text class="sign pl-2 font-16">登录/注册</text>
				</view> -->
				<view class="pl-3 pr-3 show-ls ml-3 mr-3 position-absolute left-0 right-0 mt-4">
					<view class="lineHight-3">我的数据</view>
					<view class="dfex text-center pb-3">
						<view v-if='userInfo.parentId === null' @click="open('tabbar/user/myCredit')">
							<view class="font-20 text-333333 font-weight-bold">{{notices.integralCount}}</view>
							<view class="text-999999">我的积分</view>
						</view>
						<view @click="open('tabbar/user/product')">
							<view class="font-20 text-333333 font-weight-bold">{{notices.productInformCount}}</view>
							<view class="text-999999">我的产品</view>
						</view>
						<view @click="opens('tabbar/order/index')">
							<view class="font-20 text-333333 font-weight-bold">{{notices.orderCount}}</view>
							<view class="text-999999">我的订单</view>
						</view>
					</view>
				</view>
			</view>

			<view class="mt-105 bg-white">
				<free-divider></free-divider>
				<free-divider></free-divider>
				<free-divider></free-divider>
				<free-list-item title="基础信息" showLeftIcon :showRight="false" :border="false"
					@click="open('tabbar/user/basic')">
					<text slot="icon" class="iconfont  font-lg py-1 icon-jichuguanli1"></text>
				</free-list-item>
				<free-divider></free-divider>
				<free-list-item title="我的证件" showLeftIcon :showRight="false"
					@click="open('tabbar/user/identification')">
					<text slot="icon" class="iconfont  font-lg py-1 icon-idcard-examine"></text>
				</free-list-item>
				<free-list-item title="我的品牌码" showLeftIcon :showRight="false"
					@click="open('tabbar/user/BrandCode', ifdUser.id)">
					<text slot="icon" class="iconfont font-lg py-1 icon-chanpinmaguanli"></text>
				</free-list-item>
				<free-list-item title="基地信息" showLeftIcon :showRight="false" @click="open('tabbar/user/base')">
					<text slot="icon" class="iconfont font-lg py-1 icon-guanzhuderen2"></text>
				</free-list-item>
				<free-list-item title="产品信息管理" showLeftIcon :showRight="false" @click="open('tabbar/user/product')">
					<text slot="icon" class="iconfont font-lg py-1 icon-chanpin1"></text>
				</free-list-item>
				<free-list-item v-if='userInfo.parentId === null' title="登录账户管理" showLeftIcon :showRight="false"
					@click="open('tabbar/user/administration')">
					<text slot="icon" class="iconfont font-lg py-1 icon-yonghudengluguanli"></text>
				</free-list-item>
				<free-list-item title="订单评价" showLeftIcon :showRight="false" @click="open('tabbar/user/evaluation')">
					<text slot="icon" class="iconfont font-lg py-1 icon-dingdan"></text>
				</free-list-item>
				<free-list-item title="退出登录" showLeftIcon :showRight="false" @click="update">
					<text slot="icon" class="iconfont font-lg py-1 icon-tuichu"></text>
				</free-list-item>
			</view>
		</scroll-view>
		<free-confirm ref="confirm" :title="confirmTitle">
			<view class="iconfont icon-tuichu text-center font-48 text-A0A0A0"></view>
		</free-confirm>
	</view>
</template>

<script>
	import $H from '@/common/free-lib/request.js';
	import {
		getImageUrl
	} from '@/common/free-lib/util.js';
	import freeListItem from "@/components/free-ui/free-list-item.vue";
	import freeDivider from "@/components/free-ui/free-divider.vue";
	import freeConfirm from "@/components/free-ui/free-confirm.vue";
	export default {
		components: {
			freeListItem,
			freeDivider,
			freeConfirm,
		},
		data() {
			return {
				confirmText: "",
				confirmType: "",
				userInfo: null, // 存储用户信息
				notices: {},
				label: [],
				ifdUser:''
			}
		},
		onShow() {
			this.tagList();
			const userInfo = uni.getStorageSync('userInfo');
			this.NewUser(userInfo.id)
			if (userInfo) {
				this.userInfo = userInfo;
			} else {
				uni.showToast({
					title: '请先登录',
					icon: 'none',
				});
			}
		},
		onLoad() {
			
			this.NewDataList();
			this.tagList()

		},
		computed: {
			confirmTitle() {
				return this.confirmType == 'username' ? '是否退出登录！' : '是否退出登录！'
			},
	
		},
		methods: {
			handleImageError() {
				uni.showToast({
					title: '图片格式不支持jpeg，请重新上传',
					icon: 'none',
					duration: 4000
				});
			
			},
			// 拼接图片方法
			getFullImageUrl(imagePath) {
				return getImageUrl(imagePath);
			},
			// 我的数据 
			NewDataList() {
				$H.get('/agriculture/productInform/agriculture/queryMyData', {}, {}).then(res => {
					console.log(res, 'res')
					this.notices = res.result
				}).catch(error => {
					console.log(error)
				});
			},
			// 退出登录
			logout() {
				return $H.get('/brand/user/agriculture/logout', {}, {});
			},

			update() {
				this.$refs.confirm.show((close) => {
					this.logout()
						.then(() => {
							uni.removeStorageSync('token'); // 清除缓存中的 token
							uni.navigateTo({
								url: '/pages/login/login'
							});
						})
						.catch(error => {
							console.log(error)
						})
						.finally(() => {
							close(); // 关闭确认弹框
						});
				});
			},

			// 标签
			tagList() {
				$H.get('/agriculture/agricultureLabel/mobile/agricultureById', {}, {}).then(res => {
					this.label = res.result.labelList
				}).catch(error => {
					console.log(error)
				});
			},

			open(path, id) {
				let url = '/pages/' + path;
				if (id) {
					url += '?id=' + id;
				}
				uni.navigateTo({
					url: url
				});
			},

			NewUser(id) {
				$H.get('/brand/user/queryById', {
					id: id
				}, {}).then(res => {
					this.ifdUser = res.result
				}).catch(error => {
					uni.showToast({
						title: "网络错误，请稍后再试",
						icon: 'none'
					});
				});
			},
			opens(path) {
				uni.switchTab({
					url: '/pages/' + path
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.tab-item {
		font-size: 16px;
		cursor: pointer;
		position: relative;
	}

	.tab-item.active {
		font-size: 16px;
		color: #FFC251;
	}

	.tab-item.active::after {
		content: '';
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		height: 2px;
		background-color: #FFC251;
	}

	.index-color-1 {
		font-size: 22px;
		color: #FF0000;
	}

	.index-color-2 {
		font-size: 22px;
		color: #17a2b8;
	}

	.index-color-3 {
		font-size: 22px;
		color: #FFC251;
	}
</style>